#!/usr/bin/env python
#
# Copyright (C) 2014-2020 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#
from __future__ import print_function, division, absolute_import #, unicode_literals

try:
    from ConfigParser import ConfigParser
except ImportError:
    from configparser import ConfigParser

from time import gmtime,strftime

import os
import re
import sys

class MyConfigParser(ConfigParser):

  def optionxform(self,option):
    return str(option)

# ---------------------------------------------------------------------------- #

#
# Subroutines
#

# Macro for file output
def macro_output_template(name,stamp):

  return """\
# -*- M4 -*-
#
# Copyright (C) 2014-2020 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

# Generated by %s on %s

#
# Output macros for the "configure" script
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. If you try to edit it, your changes will systematically be
# overwritten.
#



# ABIBND_OUTPUT()
# ---------------
#
# Outputs configuration for ABINIT.
#
AC_DEFUN([ABIBND_OUTPUT],[
  dnl Config files
  AC_CONFIG_FILES([@MAKEFILES@])

  AC_OUTPUT
]) # ABIBND_OUTPUT
""" % (name,stamp,name)



# Macro to check prerequisites
def macro_prereqs_template(name,stamp):

  return """\
# -*- M4 -*-
#
# Copyright (C) 2014-2020 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

# Generated by %s on %s

#
# Check macros for the "configure" script
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. If you try to edit it, your changes will systematically be
# overwritten.
#



# ABIBND_CHECK_PREREQS()
# ----------------------
#
# Check that ABINIT prerequisites are present for all bindings.
#
AC_DEFUN([ABIBND_CHECK_PREREQS],[
  dnl Init
  bnd_prereqs_ok="yes"
@PREREQS@
  dnl Abort if some ingredients are missing
  if test "${bnd_prereqs_ok}" != "yes"; then
    AC_MSG_ERROR([some prerequisites are missing
        Please configure and build Abinit with the --enable-exports option
        and copy the ~abinit/bindings/config.sh file before configuring the
        bindings])
  fi
]) # ABIBND_CHECK_PREREQS
""" % (name,stamp,name)



# ---------------------------------------------------------------------------- #

#
# Main program
#

# Initial setup
my_name    = "make-macros-output"
my_config  = "config/specs/bindings.conf"
my_outputs = {
  "out":"config/m4/auto-output.m4",
  "prq":"config/m4/auto-prereqs.m4"}

# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
     not os.path.exists("config/specs/bindings.conf") ):
  print("%s: You must be in the top of an ABINIT Bindings source tree." % my_name)
  print("%s: Aborting now." % my_name)
  sys.exit(1)

# Read config file(s)
if ( not os.path.exists(my_config) ):
    print("%s: Could not find config file (%s)." % (my_name,my_config))
    print("%s: Aborting now." % my_name)
    sys.exit(2)

# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())

# Init
cnf = MyConfigParser()
cnf.read(my_config)
subdirs = cnf.sections()
subdirs.sort()

# Generate output macro
m4  = macro_output_template(my_name,now)
lst = "\n    Makefile"
for sub in subdirs:
  lst += "\n    %s/Makefile" % (sub)
m4 = re.sub("@MAKEFILES@",lst,m4)
open(my_outputs["out"], "wt").write(m4)

# Generate prerequisites macro
m4 = macro_prereqs_template(my_name,now)
prq = ""
for sub in subdirs:
  prq += "\n  dnl Binding: %s\n" % sub + \
    "  AC_MSG_NOTICE([looking for %s prerequisites])\n" % sub
  if ( cnf.has_option(sub,"abinit") ):
    for obj in cnf.get(sub,"abinit").split():
      prq += "  AC_MSG_CHECKING([for ~abinit_builddir/%s])\n" % obj + \
             "  tmp_prereqs_chk=\"yes\"\n" + \
             "  if test ! -s \"${abinit_builddir}/%s\"; then\n" % obj + \
             "    tmp_prereqs_chk=\"no\"\n" + \
             "  fi\n" + \
             "  AC_MSG_RESULT([${tmp_prereqs_chk}])\n"
      prq += "  if test \"${tmp_prereqs_chk}\" = \"no\"; then\n" + \
             "    AC_MSG_CHECKING([for ~abinit_srcdir/%s])\n" % obj + \
             "    tmp_prereqs_chk=\"yes\"\n" + \
             "    if test ! -s \"${abinit_srcdir}/%s\"; then\n" % obj + \
             "      tmp_prereqs_chk=\"no\"\n" + \
             "      bnd_prereqs_ok=\"no\"\n" + \
             "    fi\n" + \
             "    AC_MSG_RESULT([${tmp_prereqs_chk}])\n" + \
             "  fi\n"
  if ( cnf.has_option(sub,"prereqs") ):
    for src in cnf.get(sub,"prereqs").split():
      prq += "  AC_MSG_CHECKING([for %s/%s])\n" % (sub,src) + \
             "  tmp_prereqs_chk=\"yes\"\n" + \
             "  if test ! -s \"${ac_top_srcdir}/%s/%s\"; then\n" % (sub,src) + \
             "    tmp_prereqs_chk=\"no\"\n" + \
             "    bnd_prereqs_ok=\"no\"\n" + \
             "  fi\n" + \
             "  AC_MSG_RESULT([${tmp_prereqs_chk}])\n"
m4 = re.sub("@PREREQS@",prq,m4)
open(my_outputs["prq"], "wt").write(m4)
